package it.ipermemory.utilities;

import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

/**
 * It allows you to control playback of multimedia content such as audio and
 * video by managing their states (play, pause and stop).
 * 
 * @author Gianluca Carlesso (gianluca.carlesso@gmail.com)
 * @version 1.0
 * 
 */
public class MediaControllerPlayer {
	/**
	 * Indicates whether the content is playing
	 */
	private boolean isPlay = true;

	/**
	 * Object that contains the button for starting the multimedia content
	 */
	final private ImageButton play_button;

	/**
	 * Object that contains the button to pause the multimedia content
	 */
	final private ImageButton pause_button;

	/**
	 * Object that contains the button to stop the multimedia content
	 */
	final private ImageButton stop_button;

	/**
	 * Object to display the elapsed time of the play
	 */
	final private TextView current_time;

	/**
	 * Object to display the total time of the play
	 */
	final private TextView total_time;

	/**
	 * Object that handles the media
	 */
	private MediaPlayer mplayer;

	/**
	 * Represents the state of play
	 */
	final private int STATUS_PLAY = 0;

	/**
	 * Represents the state of pause
	 */
	final private int STATUS_PAUSE = 1;

	/**
	 * Constructor that initializes a new object for managing multimedia content
	 * 
	 * @param mp
	 *            object initialized with the media
	 * @param play_button
	 *            contains the button for playing the multimedia content
	 * @param pause_button
	 *            contains the button to pause the multimedia content
	 * @param stop_button
	 *            contains the button to stop the multimedia content
	 * @param total_time
	 *            to display the total time of the play
	 * @param current_time
	 *            to display the elapsed time of the play
	 */
	public MediaControllerPlayer(MediaPlayer mp, final ImageButton play_button,
			final ImageButton pause_button, final ImageButton stop_button,
			final TextView total_time, final TextView current_time) {
		this.mplayer = mp;
		this.total_time = total_time;
		this.current_time = current_time;
		this.play_button = play_button;
		this.pause_button = pause_button;
		this.stop_button = stop_button;
		stop_button.setEnabled(false);

		// Action to be performed to determine the content
		this.mplayer.setOnCompletionListener(new OnCompletionListener() {

			public void onCompletion(MediaPlayer mp) {
				isPlay = true;
				setImagePlayPause(STATUS_PAUSE);
				stop_button.setEnabled(false);
			}
		});
	}

	/**
	 * Stops playback of multimedia content
	 * 
	 * @throws IllegalStateException
	 *             raised in the event of an error in the handling of the media
	 */
	public void stopMedia() throws IllegalStateException {
		// MediaPlayer reset for a new play
		mplayer.seekTo(0);
		mplayer.stop();
		mplayer.prepareAsync();

		// Reset the visual elements
		isPlay = true;
		setImagePlayPause(STATUS_PAUSE);

		play_button.setEnabled(false);
		stop_button.setEnabled(false);
	}

	/**
	 * Management of the states of play and pause the media
	 */
	public void playPause() {
		// Manage media & background image button
		if (isPlay) {
			setImagePlayPause(STATUS_PLAY);
			isPlay = false;
			mplayer.start();
		} else {
			setImagePlayPause(STATUS_PAUSE);
			isPlay = true;
			mplayer.pause();
		}

		stop_button.setEnabled(true);
	}

	/**
	 * Release the media, in particular deals with the release of the occupied
	 * memory for playback
	 */
	public void releaseMedia() {
		// Control that was initialized at least once and that has not already
		// been issued
		if (mplayer != null) {
			mplayer.stop();
			mplayer.release();
			mplayer = null;
		}
	}

	/**
	 * Handles the two buttons: play and pause. I own are reciprocally
	 * exclusive, if visuallizato one, the other must be hidden
	 * 
	 * @param STATUS
	 *            which was to be led display button
	 */
	private void setImagePlayPause(final int STATUS) {
		switch (STATUS) {
		case 0:
			play_button.setVisibility(View.GONE);
			pause_button.setVisibility(View.VISIBLE);
			break;
		default:
			play_button.setVisibility(View.VISIBLE);
			pause_button.setVisibility(View.GONE);
			break;
		}
	}

	/**
	 * Enable/Disable the button to play
	 * 
	 * @param value
	 *            if true the button will be enabled, if false will disable
	 */
	public void setEnabledPlayButton(boolean value) {
		play_button.setEnabled(value);
	}

	/**
	 * Calculated in the format minutes:seconds the duration of the average
	 * 
	 * @return a string that represents the total time of the media
	 */
	public String getTotalTime() {
		int minute = (int) (mplayer.getDuration() / 1000) / 60;
		int second = (mplayer.getDuration() / 1000) - minute * 60;

		String time = "";
		if (minute < 10) {
			time += "0";
		}

		time += minute + ":";

		if (second < 10) {
			time += "0";
		}
		return time + second;
	}

	/**
	 * Calculate the time spent in play
	 * 
	 * @return a string that represents the time spent in play
	 */
	public String getCurrentTime() {
		int minute = (int) (mplayer.getCurrentPosition() / 1000) / 60;
		int second = (mplayer.getCurrentPosition() / 1000) - minute * 60;

		String time = "";
		if (minute < 10) {
			time += "0";
		}

		time += minute + ":";

		if (second < 10) {
			time += "0";
		}
		return time + second;
	}

	/**
	 * Sets the time spent in the display object that represents graphically
	 */
	public void setCurrentTime() {
		current_time.setText(getCurrentTime());
	}

	/**
	 * Set the total time in which the object is displayed graphically
	 */
	public void setTotalTime() {
		total_time.setText(getTotalTime());
	}
}
